class Array
  def second
    self[1]
  end
  def third
    self[2]
  end
end

class Object
  def atom?
    false
  end
end
class String
  def atom? 
    true
  end
end
class Float
  def atom? 
    true
  end
end
class Symbol
  def atom? 
    true
  end
end
class Fixnum
  def atom? 
    true
  end
end
class NilClass
  def atom? 
    true
  end
end

class Object
  def self.inherited(subclass)
    if @subclasses
      @subclasses << subclass
      else
      @subclasses = [subclass]
      end
    end

  def self.subclasses
    @subclasses
  end

  ### Deep snapshotting of specific objects

  def snapshot
    # Marshal.load( Marshal.dump( self ) )
    self.clone
  end
  
  def restore_from_snapshot(obj)
    instance_variables.each do |ivar|
      instance_variable_set(ivar, obj.instance_variable_get(ivar))
    end
    self
  end
end

## loaned from merb

class String
  # "FooBar".snake_case #=> "foo_bar"
  def snake_case
    gsub(/\B[A-Z]/, '_\&').downcase
  end

  # "foo_bar".camel_case #=> "FooBar"
  def camel_case
    split('_').map{|e| e.capitalize}.join
  end

  def html_safe
    gsub("<","&lt;").gsub(">","&gt;")
  end

  # Concatenates a path  
  def /(o)
    File.join(self, o.to_s)
  end

end     

### Time

class Time
  def to_date
    Date.new(year, month, day)
  end

  def days_in_month
    date = at_beginning_of_month
    curr_month = date.month
    days = []
    while date.month == curr_month do
      days << date
      date = date.advance(:days=>1)
    end
    days
  end

  def week_day
    res = wday-1
    if res == -1
      6
    else
      res
    end
  end

  def week 
    to_date.cweek
  end

  ## epoch is the day number counted in days since Jan 1 1970

  def to_epoch
    ((self - Time.at(0))/(60*60*24)).truncate
  end

  def self.from_epoch(epoch)
    Time.at(Time.at(0)+epoch*60*60*24)
  end
end

class Object
  def time_from_epoch
    Time.from_epoch(self.to_i)
  end
end


class Array

  def index_of
    each_with_index do |ea, index|
      return index if yield(ea)
    end
    return nil
  end

  def random
    self[rand(self.size)]
  end

  def pos
    callcc {|k| self.inject(0) {|memo, ea| yield(ea) ? k.call(memo) : memo+1 } ; nil }
  end
end
